package com.google.code.mamule.mail;

import com.google.code.mamule.order.Order;
import com.google.code.mamule.user.User;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface MailRepository extends CrudRepository<Mail, Long> {

  Mail findByIdAndOwner(Long id, User owner);

  @Query("SELECT m FROM Mail m WHERE m.owner = :owner")
  Iterable<Mail> findAllFrom(@Param("owner") User owner);
  
  @Query("SELECT o FROM Order o WHERE :m MEMBER OF o.items ")
  Order findOrder(@Param("m") Mail m);
}
